package source;

/*
 * Author: Aqeel Kazmi
 * Email: aqeelhkazmi@gmail.com
*/

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

import javax.media.protocol.*;
import javax.media.rtp.*;
import javax.media.rtp.event.*;
import javax.media.*;
import java.net.*;

public class RemoteFile extends MouseAdapter implements ActionListener
{
	
	Frame d = null;
	MediaPlayerFrame mpf = null;
	java.awt.List songs = null;
	Properties ht = null;
	String name = null;
	JButton btplay = null;
	JButton btrefresh = null;
	JButton btclose = null;
	
	String ipp = null;
	int portt = 0;
	int playing = 0;
	
	public RemoteFile(MediaPlayerFrame mpf)
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		this.mpf = mpf;
		songs = new java.awt.List(6,false);
		songs.addMouseListener(this);
		ht = new Properties();
		d = new Frame();
		name = "Available Files";
		setButtons();
		process();		
		d.setSize(230,300);
		d.setResizable(false);
		d.setLocation(350,150);
		//d.setVisible(true);
	}
	
	public void setButtons()
	{
		btplay = new JButton("Play");
		btplay.setActionCommand("Play");
		btplay.addActionListener(this);
		
		btrefresh = new JButton("Refresh");
		btrefresh.setActionCommand("Refresh");
		btrefresh.addActionListener(this);
		
		btclose = new JButton("Close");
		btclose.setActionCommand("Close");
		btclose.addActionListener(this);
		
	}
	
public void sendRequest(String ip,int port) {
			
			songs.clear();
			ipp= ip;
			portt=port;
		try {
			System.out.println("Send Request is called");
			Socket s = new Socket (ip,port);
			InputStream is = s.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			
			OutputStream os = s.getOutputStream();
			PrintWriter pw = new PrintWriter(os,true);
			String st ="os";
			pw.println(st);
			
			String length = br.readLine();
			int len=Integer.parseInt(length);
			
			String songlist[] = new String[len];
			
			for(int i=0; i<len; i++){
			String msg = br.readLine();
			songlist[i]=msg;
			//System.out.println(songlist[i]);
			}
			
			for(int i=0; i<songlist.length; i++){
				ht.put(String.valueOf(songs.getItemCount()),songlist[i]);			
				songs.add(songlist[i]);
			}			
			s.close();
		}
		catch (Exception ex) {
			System.out.println(ex);
		}
		process();
		d.setVisible(true);

	}

	public void process()
	{
		d.removeAll();
		d.setLayout(new BoxLayout(d,BoxLayout.X_AXIS));	
		d.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent we)
			{
				d.setVisible(false);
			}
		}
		);
		JPanel jp = new JPanel();
		jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));

		JPanel bt = new JPanel();
		bt.setLayout(new BoxLayout(bt,BoxLayout.X_AXIS));

		btplay.updateUI();
		btrefresh.updateUI();
		btclose.updateUI();

		bt.add(btplay);
		bt.add(btrefresh);
		bt.add(btclose);
		
		jp.add(songs);
		jp.add(bt);
		
		d.setTitle(name);
		d.setFocusable(true);
		d.addKeyListener(new KeysListen(mpf));				
		d.add(jp);		
		
	}
	public void playSong(int index)
	{	
		String ss = (String)ht.get(String.valueOf(index));
		if(ss==null)
		return;
		try
			{
			System.out.println(ss);
			Socket sss = new Socket (ipp,portt);
			OutputStream os = sss.getOutputStream();
			PrintWriter pw = new PrintWriter(os,true);
			pw.println(ss);
			
			try {
			    Thread.currentThread().sleep(1000);
			} catch (InterruptedException ie) {
				System.out.println(ie);
			}
		    String url = "rtp://137.43.200.143:5000/audio";  //here we will use the data sink created by server.
			MediaLocator ml = new MediaLocator(url);
		    mpf.setLocator(ml);
		    		
				sss.close();
			}
				catch(Exception e){
					System.out.println(e);
		}
	}



	public int totalSongs()
	{		
		
		int count = songs.getItemCount();
		return count;
	}

	public void refresh()
	{
		if(d.isVisible()==true)
		{
			d.setVisible(false);
			d.setVisible(true);
		}		
	}


	public void actionPerformed(ActionEvent ae)
	{
		String check = ae.getActionCommand();
		if(check.equals("Refresh"))
		{
			refresh();
		}
		
		if(check.equals("Close"))
			{
	    	d.dispose();
			}
		if(check.equals("Play"))
		{	
			if(songs.getSelectedIndex()==-1)
			return;			
			playing = songs.getSelectedIndex();
			playSong(playing);
		}
	}

	public void mouseClicked(MouseEvent me)
	{
		if(me.getClickCount()==2 && me.getButton()==1)
		{
		if(songs.getSelectedIndex()==-1)
		return;			
		playing = songs.getSelectedIndex();
		playSong(playing);
		}
		else
		return;
	}

}